<?php
	session_start();
	require('gen_functions.php');
	$show_form = true;
	$show_cancel = true;
	$usermatch = false;
	$user_bad = false;
	$name_bad = false;
	$pass1_bad = false;
	$pass2_bad = false;
	
	if($_POST['submit'] != ''){
		$show_form = false;
		
		//Test to see if the user name is used
		$user = clean_data($_POST['user']);
		$user_id = sha1($user);
		$name = clean_data($_POST['name']);
		$pass1 = clean_data($_POST['pass1']);
		$pass2 = clean_data($_POST['pass2']);
		$email = clean_data($_POST['email']);
		$website = clean_data($_POST['website']);
		
		if($user == ''){
			echo 'Please enter a User name<br>';
			$user_bad = true;
		}
		if($name == ''){
			echo 'Please enter your name<br>';
			$name_bad = true;
		}
		
		
		dblogin();
		$query = 
		"select login from PLAYERS where login_id = '$user_id'";
		if($result = mysql_query($query)){
			if($row = mysql_fetch_array($result)){
				$show_form = true;
				echo 'User name already in database<br>';
				$user_bad = true;
				$usermatch = true;
			}
		}
		
		//Test to see if pass's match
		if($pass1==$pass2 && $pass1 != '' && !$usermatch){
			$show_cancel = false;
			//Add info to the databse
			$sha1ed_pass = sha1($pass1);
			$query=
			"insert into PLAYERS (LOGIN,LOGIN_ID,NAME,PASS,ABOUT,STATUS,EMAIL,HTML,TOT_GP,SIGNUP,LST_LOGIN,LST_VOTE) values 
			('$user','$user_id','$name','$sha1ed_pass','','G','$email','$html',0,sysdate(),sysdate(),sysdate())";
			if($result = mysql_query($query)){
				echo "QUERY SUCESSFUL<br>";
			}else{
				echo 'ERROR in QUERY<br>';
				echo $query,'<br>';
			}

			echo 'Congratulations, you have been added to the database!<br>';
			echo '<input type="submit" name="OK" value="Ok"  onclick="self.parent.tb_remove();"/>';
		}else if($pass1 == '' || $pass2 == ''){
			echo 'You have a blank password, please try again<br>';
			$show_form = true;
			if($pass1 == '')$pass1_bad = true;
			if($pass2 == '')$pass2_bad = true;
		}else if(!$usermatch){
			echo 'Your passwords don\'t match, please try again<br>';
			$show_form = true;
			$pass1_bad = true;
			$pass2_bad = true;
		}		
	}

	
?>
<html>
<head>
<!-- Jquery stuff -->
<link rel="stylesheet" href="../images/signup.css" type="text/css" />
<script type="text/javascript" src="./jquery/jquery.js"></script>
<script type="text/javascript" src="./jquery/thickbox.js"></script>
<link rel="stylesheet" href="jquery/thickbox.css" type="text/css" media="screen" />

<title>FriNiteTourny</title>
</head>
<body>

<?php if($show_form){ ?>
<form class="align-left" action="#" method="post">
<div id="signup">
	<label> Login*:</label>
	<input <?php if($user_bad){ ?> class="error" <?php }?>type="text" name="user"  value="<?php echo $user ?>" />
	<label> Name*:</label>
	<input <?php if($name_bad){ ?> class="error" <?php }?>type="text" name="name" value="<?php echo $name ?>" />
	<label> Password*:</label>
	<input <?php if($pass1_bad){ ?> class="error" <?php }?> type="password" name="pass1" value="" />
	<label> Again*:</label>
	<input <?php if($pass2_bad){ ?> class="error" <?php }?> type="password" name="pass2" value="" />
	<label> Email:</label>
	<input type="text" name="email" value="<?php echo $email ?>" />
	<label> Website:</label>
	<input type="text" name="website" value="<?php echo $website ?>" />
	<input type="submit" name="submit" value="submit" />
<?php if($show_cancel){ ?>
<input type="submit" name="Cancel" value="Cancel"  onclick="self.parent.tb_remove();"/>
<?php } ?>
	<p style="color:#FF0000">* denotes required</p>
</div>
</form>
<?php } ?>



</body>